package cn.bby.tools.actions;

import cn.bby.tools.utils.AnActionEventUtils;
import cn.hutool.core.util.StrUtil;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import org.jetbrains.annotations.NotNull;

import static cn.bby.tools.utils.ClipboardUtil.copyAsColumnSelection;

/**
 * @author ben
 * @since 2025/9/24 23:29
 */
public class GenErrorCodeAction extends AnAction {
    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        String selectedText = AnActionEventUtils.getSelectedText(e);
        if (StrUtil.isEmpty(selectedText)) {
            return;
        }
        if (!StrUtil.isNumeric(selectedText)) {
            return;
        }
        if (selectedText.length() > String.valueOf(Long.MAX_VALUE).length()){
            return;
        }
        long errorCode = Long.parseLong(selectedText);
        int size = 50;
        String[][] errorCodes = new String[size][1];
        for (int i = 1; i <= size; i++) {
            String errorCodeStr = String.valueOf(errorCode+i);
            errorCodes[i-1][0] = errorCodeStr;
        }
        copyAsColumnSelection(errorCodes);
    }

    @Override
    public void update(@NotNull AnActionEvent e) {
        String selectedText = AnActionEventUtils.getSelectedText(e);
        if (StrUtil.isEmpty(selectedText)) {
            e.getPresentation().setEnabledAndVisible(false);
            return;
        }
        if (!StrUtil.isNumeric(selectedText)) {
            e.getPresentation().setEnabledAndVisible(false);
            return;
        }
    }
}
